const userModel = require('../../model/users/userModel')
class userService{
  //获取所有用户信息
  async getUsers(req,res){
    
    const {results:data} = await userModel.users()
    if(data.length != 0){
      return res.json({
        data,
        meta: {
          msg: '用户信息获取成功',
          status: 200
        } 
      })
    }else{
      return res.json({
        meta: {
          msg: '用户信息获取失败',
          status: 404
        }
      })
    }
  }
  
  //查询用户 query pagenum第几页 pagesize每页几人
  async postUsers(req,res){
    let query = req.body.query
    let pagenum = parseInt(req.body.pagenum)
    let pagesize = parseInt(req.body.pagesize)
    if(pagesize === 1){
      pagenum = pagenum-1
    }else if(pagesize === 2){
      pagenum = (pagenum-1)*2
    }else if(pagesize === 5){
      pagenum = (pagenum-1)*5
    }else if(pagesize === 10){
      pagenum = (pagenum-1)*10
    }
    const {results:data1} = await userModel.queryUsers(query,pagenum,pagesize)
    const {results:data2} = await userModel.users()
    let datas = {
      'pagenum':pagenum,
      'total':data2.length,
      'users':data1,
    }
    if(datas.length != 0){
      return res.json({
        datas,
        meta: {
          msg: '获取用户信息获取成功',
          status: 200
        } 
      })
    }else{
      return res.json({
        meta: {
          msg: '获取用户信息获取失败',
          status: 404
        }
      })
    }
    
  }

  //添加用户 username,password,email,phone,position
  async postAddUser(req,res){
    let account = req.body.account
    let username = req.body.username
    let password = req.body.password
    let email = req.body.email
    let phone = req.body.phone
    let position = req.body.position
    let sdtnbr = req.body.sdtnbr;
    const {results:data} = await userModel.queryAddUser(account)
    console.log(data)
    console.log(data.length)
    if(data.length == 0 ){
      const results = await userModel.addUser(account,username,password,email,phone,position,sdtnbr)
      return res.json({
        meta:{
          msg: '添加新用户成功',
          status: 200
        }
      })
    }else{
      return res.json({
        meta: {
          msg: '添加新用户失败',
          status: 404
        }
      })
    }

  }
  //修改用户信息  account,username,password,email,phone,position,id
  async postUpdateUser(req,res){
    let account = req.body.account
    let username = req.body.username
    let password = req.body.password
    let email = req.body.email
    let phone = req.body.phone
    let position = req.body.position
    let sdtnbr = req.body.sdtnbr
    let id = req.body.id
    console.log(sdtnbr+'夏星')
    console.log(username)
    const {results:data} = await userModel.updateUser(account,username,password,email,phone,position,sdtnbr,id)
    console.log(data)
    if(data.length != 0){
      
      return res.json({
        meta:{
          msg: '修改用户信息成功',
          status: 200
        }
      })
    }else{
      return res.json({
        meta: {
          msg: '修改用户信息失败',
          status: 404
        }
      })
    }
  }
  async postUpdateUseres(req,res){
    let account = req.body.account
    let username = req.body.username
    let email = req.body.email
    let phone = req.body.phone
    let id = req.body.id
    const {results:data} = await userModel.queryAddUser(account)
    if(data.length != 0){
      const results = await userModel.updateUseres(username,phone,email,id)
      return res.json({
        meta:{
          msg: '修改用户信息成功',
          status: 200
        }
      })
    }else{
      return res.json({
        meta: {
          msg: '修改用户信息失败',
          status: 404
        }
      })
    }
  }

  //删除用户
  async postDeleteUser(req,res){
    let account = req.body.account
    let id = req.body.id
    const {results:data} = await userModel.queryAddUser(account)
    if(data.length != 0 ){
      const deleteID = await userModel.deleteUser(id)
      return res.json({
        data,
        meta:{
          msg: '删除用户信息成功',
          status: 200
        }
      })
    }else{
      return res.json({
        meta:{
          msg: '删除用户信息失败',
          status: 404
        }
      })
    }
    

  }
}

module.exports = new userService()